<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<!-- bootstarp-fileinput -->
	<link rel="stylesheet" type="text/css" href="../js/bootstrap-fileinput/css/fileinput.min.css"/>
	<script type="text/javascript" src="../js/bootstrap-fileinput/js/fileinput.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-fileinput/js/locales/zh.js"></script>
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody>
			                          <tr th:each="c : ${cs}">
			                              <td><input  type="checkbox"></td>
				                          <td th:text="${c.id}">1</td>
									      <td th:text="${c.categoryId}">1</td>
									      <td th:text="${c.title}">促销海报1</td>
									      <td th:text="${c.url}">http://wwww.hb.com/hd1.html</td>
									      <td>
									      	<img alt="" th:src="${c.pic}" width="100px" height="50px">
									      </td>
									      <td th:text="${c.sortOrder}">1</td>
									      <td th:if="${c.status eq '1'}">有效</td>
									      <td th:if="${c.status eq '0'}">无效</td>
		                                  <td class="text-center">
		                                 	  <button type="button" onclick="initUpdatePage(this)" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal">修改</button>
		                                  </td>
			                          </tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" id="select">
							<option th:each="cc : ${ccs}" th:text="${cc.name}" th:value="${cc.id}"></option>
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" placeholder="标题" id="titleId">  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" id="urlId">  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" placeholder="排序" id="orderId">  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
									<input type="file" id="myFile" name="myFile" multiple />
									<input type="text" name="rimage" id="fileAddr" value="">
								</td>
							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" class="icheckbox_square-blue" checked="checked" id="statusId">
		      		</td>
		      	</tr>  	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addContent()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel2">广告修改</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <input type="hidden" id="contentId">
							  <select class="form-control" id="select2">
								  <!--<option th:each="cc : ${ccs}" th:text="${cc.name}" th:value="${cc.id}"></option>-->
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input  class="form-control" placeholder="标题" id="titleId2">  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input  class="form-control" placeholder="URL" id="urlId2">  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input  class="form-control" placeholder="排序" id="orderId2">  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table>
								  <tr>
									  <td>
										  <img src="" width="100px" height="100px" id="imageId">
										  <input type="file" id="myFile2" name="myFile" multiple />
										  <input type="text" name="rimage" id="fileAddr2" value="">
									  </td>
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" class="icheckbox_square-blue" checked="checked" id="statusId2">
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateContent()">修改</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>
<script>
	function addContent(){
		var title = $("#titleId").val();
		var url = $("#urlId").val();
		var sortOrder = $("#orderId").val();
		var pic = $("#fileAddr").val();
		var option = $("#select option:selected");
		var categoryId = option.val();
		var status;
		if($("#statusId").attr("checked")){
			status = 1;
		}else{
			status = 0;
		}

		$.ajax({
			url:"/content/addContent",
			type:"post",
			dataType:"json",
			data:{title:title,url:url,sortOrder:sortOrder,pic:pic,status:status,categoryId:categoryId},
			aysnc:false,
			success:function(){
				window.location.reload();
			},
			error:function(){
				alert("系统异常");
			}
		})

	}


	$(function(){
		fileInit();
		fileInit2();
	})
	//初始化上传插件 及上传路径和回显配置   增加页面
	function fileInit(){
		$('#myFile').fileinput({
			language: 'zh',
			uploadUrl: '/content/upLoadFile',
			maxFileCount :3, // multiple
			enctype : 'multipart/form-data',
			maxFileSize : 1024, //单位为kb
			allowedFileExtensions : ['jpg', 'gif', 'png'],//限制上传文件类型
			browseClass: 'btn btn-primary'
		});

		$('#myFile').on('fileuploaded',function(event, data, previewId, index){
			//data.response 得到的就是后台return返回的值
			var result = data.response;
			var url = result.data;
			$("#fileAddr").val(url);
		})
	}

	//初始化上传插件 及上传路径和回显配置  修改页面
	function fileInit2(){
		$('#myFile2').fileinput({
			language: 'zh',
			uploadUrl: '/content/upLoadFile',
			maxFileCount :3, // multiple
			enctype : 'multipart/form-data',
			maxFileSize : 1024, //单位为kb
			allowedFileExtensions : ['jpg', 'gif', 'png'],//限制上传文件类型
			browseClass: 'btn btn-primary'
		});

		$('#myFile2').on('fileuploaded',function(event, data, previewId, index){
			//data.response 得到的就是后台return返回的值
			var result = data.response;
			var url = result.data;
			$("#fileAddr2").val(url);
		})
	}

	function initUpdatePage(obj){
		var tr = $(obj).parent().parent();
		var tdArr = $(tr).children();
		var id = tdArr.eq(1).html();
		$.ajax({
			url:"/content/findContentById",
			type:"post",
			dataType:"json",
			data:{id:id},
			aysnc:false,
			success:function(result){
				var map = result.data;
				var content = map.content;
				$("#titleId2").val(content.title);
				$("#urlId2").val(content.url);
				$("#orderId2").val(content.sortOrder);
				$("#fileAddr2").val(content.pic);
				$("#imageId").attr("src",content.pic);
				$("#contentId").val(content.id);
				var ccs = map.ccs;
				var info = '';
				$(ccs).each(function(i,e){
					if(content.categoryId == e.id){
						info += '<option value="'+e.id+'" selected="selected">'+e.name+'</option>';
					}else{
						info += '<option value="'+e.id+'">'+e.name+'</option>';
					}
				})
				$("#select2").html(info);
			},
			error:function(){
				alert("系统异常");
			}
		})
	}

	function updateContent(){
		var title = $("#titleId2").val();
		var url = $("#urlId2").val();
		var sortOrder = $("#orderId2").val();
		var pic = $("#fileAddr2").val();
		var id = $("#contentId").val();
		var status;
		if($("#statusId2").prop("checked")){
			status = 1;
		}else{
			status = 0;
		}
		var option = $("#select2 option:selected");
		var categoryId = option.val();
		console.log(categoryId);
		$.ajax({
			url:"/content/updateContentById",
			type:"post",
			dataType:"json",
			data:{id:id,title:title,url:url,sortOrder:sortOrder,pic:pic,status:status,categoryId:categoryId},
			aysnc:false,
			success:function(){
				window.location.reload();
			},
			error:function(){
				alert("系统异常");
			}
		})

	}
</script>
</html>